package employee;

import java.time.LocalDate;
import java.util.Date;
import java.util.Objects;

/**
 * 员工工资类
 */
public class Employee {
    /**
     * 员工编号
     */
    protected final int id;
    /**
     * 姓名
     */
    protected final String name;
    /**
     * 出生日期
     */
    protected final Date dateBirth;
    /**
     * 入职年月
     */
    protected final Date dateOnboarding;
    /**
     * 每月应发工资
     */
    protected int wages;


    /**
     * 总工资
     */
    protected int totalSalary = 0;

    /**
     * @param id 员工编号
     * @param name 姓名
     * @param dateBirth 出生日期
     * @param dateOnboarding 入职年月
     * @param wages 每月应发工资
     */
    public Employee(int id, String name, Date dateBirth, Date dateOnboarding, int wages) {
        this.id = id;
        this.name = name;
        this.dateBirth = dateBirth;
        this.dateOnboarding = dateOnboarding;
        this.wages = wages;
    }

    /**
     * 支持链式编程的计算工资的方法
     * 根据参数年份和月份来计算某年某月工资，该员工入职时间每多满1年则每月增加2元的工龄工资;
     * 如果该月员工过生日，则公司会额外奖励200元。
     * @param year 年份
     * @param month 月份
     * @return 该类本身
     */
    public Employee setSalary(int year, int month){
        if (month - 1 == dateBirth.getMonth()){
            totalSalary += 200;
        }

        int count  = year - dateOnboarding.getYear();
        wages += count * 2;

        totalSalary += wages;

        return this;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Date getDateBirth() {
        return dateBirth;
    }

    public Date getDateOnboarding() {
        return dateOnboarding;
    }

    public int getTotalSalary() {
        return totalSalary;
    }

    public int getWages() {
        return wages;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (!(o instanceof Employee)) return false;
        Employee employee = (Employee) o;

        return getId() == employee.getId() &&
                getWages() == employee.getWages() &&
                getTotalSalary() == employee.getTotalSalary() &&
                Objects.equals(getName(), employee.getName()) &&
                Objects.equals(getDateBirth(), employee.getDateBirth()) &&
                Objects.equals(getDateOnboarding(), employee.getDateOnboarding());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getName(), getDateBirth(), getDateOnboarding(), getWages(), getTotalSalary());
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", dateBirth=" + dateBirth +
                ", dateOnboarding=" + dateOnboarding +
                ", wages=" + wages +
                ", totalSalary=" + totalSalary +
                '}';
    }
}
